package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-master/cache"
	"order-master/models"
	"strconv"
	"time"
)

func WareHouseAdd(c *gin.Context) {
	wareHouse_name := c.PostForm("wareHouse_name")
	wareHouse_code := c.PostForm("wareHouse_code")
	if wareHouse_name == "" || wareHouse_code == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.WareHouse{
		WareHouseName: wareHouse_name,
		WareHouseCode: wareHouse_code,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	wid, _ := strconv.Atoi(c.PostForm("wid"))
	order_name := c.PostForm("order_name")
	order_code := c.PostForm("order_code")
	order_num, _ := strconv.Atoi(c.PostForm("order_num"))
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	if wid == 0 || order_name == "" || order_code == "" || order_num == 0 || order_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Order{
		Wid:        wid,
		OrderName:  order_name,
		OrderCode:  order_code,
		OrderNum:   order_num,
		OrderPrice: order_price,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var data []models.OrderList
	/*
		SELECT
		  orders.id AS OrderId,
		  ware_houses.ware_house_name AS WareHouseName,
		  ware_houses.ware_house_code AS WareHouseCode,
		  orders.order_name AS OrderName,
		  orders.order_code AS OrderCode,
		  orders.order_num AS OrderNum,
		  orders.order_price AS OrderPrice
		FROM
		  orders
		  LEFT JOIN ware_houses ON orders.wid = ware_houses.id
	*/
	redis_key := "order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Joins("LEFT JOIN ware_houses ON orders.wid = ware_houses.id").
			Select(
				"orders.id AS OrderId",
				"ware_houses.ware_house_name AS WareHouseName",
				"ware_houses.ware_house_code AS WareHouseCode",
				"orders.order_name AS OrderName",
				"orders.order_code AS OrderCode",
				"orders.order_num AS OrderNum",
				"orders.order_price AS OrderPrice",
			).Offset(offset).
			Limit(size).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  data,
		"count": count,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var data models.OrderList
	/*
		SELECT
		  orders.id AS OrderId,
		  ware_houses.ware_house_name AS WareHouseName,
		  ware_houses.ware_house_code AS WareHouseCode,
		  orders.order_name AS OrderName,
		  orders.order_code AS OrderCode,
		  orders.order_num AS OrderNum,
		  orders.order_price AS OrderPrice
		FROM
		  orders
		  LEFT JOIN ware_houses ON orders.wid = ware_houses.id
	*/
	redis_key := "order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Joins("LEFT JOIN ware_houses ON orders.wid = ware_houses.id").
			Select(
				"orders.id AS OrderId",
				"ware_houses.ware_house_name AS WareHouseName",
				"ware_houses.ware_house_code AS WareHouseCode",
				"orders.order_name AS OrderName",
				"orders.order_code AS OrderCode",
				"orders.order_num AS OrderNum",
				"orders.order_price AS OrderPrice",
			).
			Where("orders.id = ?", id).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": data,
	})
	return
}
